package com.thinkgem.jeesite.modules.borrow.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.borrow.entity.BorrowCount;
import com.thinkgem.jeesite.modules.borrow.entity.BorrowCountByUserRoles;
import com.thinkgem.jeesite.modules.borrow.entity.BorrowLog;
import com.thinkgem.jeesite.modules.borrow.service.BorrowLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 借阅历史Controller
 * @author zhangchong
 * @version 2013-8-29
 */
@Controller
@RequestMapping(value = "${adminPath}/book/borrowed")
public class BorrowLogController extends BaseController {

	@Autowired
	private BorrowLogService borrowLogService;

	@ModelAttribute
	public BorrowLog get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return borrowLogService.get(id);
		}else{
			return new BorrowLog();
		}
	}

	/**
	 * 日志列表
	 *
	 * @param borrowLog
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"current", ""})
	public String list(BorrowLog borrowLog, HttpServletRequest request, HttpServletResponse response, Model model){
		Page<BorrowLog> page = borrowLogService.findPage(new Page<>(request, response), borrowLog);
		model.addAttribute("page", page);
		return "modules/borrow/logList";
	}

	@RequestMapping(value = "exportExcel")
	public void exportExcel(BorrowLog borrowLog, HttpServletResponse response){
		List<BorrowLog> logs = borrowLogService.findList(borrowLog);
		try{
			String fileName = "借阅日志" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
			new ExportExcel("借阅日志" + borrowLog.getStartTime() + "-" + borrowLog.getEndTime(), BorrowLog.class)
					.setDataList(logs).write(response, fileName).dispose();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 流通汇总
	 *
	 * @param borrowCount
	 * @param model
	 * @return
	 */
	@RequestMapping("findcurrentcount")
	public String findCurrentCount(BorrowCount borrowCount, Model model){
		BorrowCount retBorrowCount = new BorrowCount();
		borrowCount.setDoType("0");
		retBorrowCount.setBorrowed(borrowLogService.findCurrentCount(borrowCount).getBorrowed());
		borrowCount.setDoType("1");
		retBorrowCount.setReturned(borrowLogService.findCurrentCount(borrowCount).getBorrowed());
		borrowCount.setDoType("2");
		retBorrowCount.setContinued(borrowLogService.findCurrentCount(borrowCount).getBorrowed());
		retBorrowCount.setReadernum(borrowLogService.findReaderNum(borrowCount).getReadernum());
		retBorrowCount.setBeginDate(borrowCount.getBeginDate());
		retBorrowCount.setEndDate(borrowCount.getEndDate());
		model.addAttribute("retBorrowCount", retBorrowCount);
		return "modules/borrow/circulationCount";
	}

	/**
	 * 期间借阅流通查询（按读者角色）
	 *
	 * @param borrowCountByUserRoles
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"borrowCountByUserRoles"})
	public String borrowCountByUserRoles(BorrowCountByUserRoles borrowCountByUserRoles, Model model){
		List<BorrowCountByUserRoles> retBorrowCountByUserRolesList = borrowLogService.findBorrowCountByUserRoles(borrowCountByUserRoles);
		borrowCountByUserRoles.setBeginDate(borrowCountByUserRoles.getBeginDate());
		borrowCountByUserRoles.setEndDate(borrowCountByUserRoles.getEndDate());
		model.addAttribute("retBorrowCountByUserRolesList", retBorrowCountByUserRolesList);
		model.addAttribute("retBorrowCountByUserRolesDate", borrowCountByUserRoles);
		return "modules/borrow/borrowCountByUserRoles";
	}
}
